package de.tu_darmstadt.gdi1.towerdefense.ui;

import java.util.Random;

import de.tu_darmstadt.gdi1.towerdefense.exceptions.InternalFailureException;
import de.tu_darmstadt.gdi1.towerdefense.exceptions.InvalidOperationException;
import de.tu_darmstadt.gdi1.towerdefense.exceptions.ParameterOutOfRangeException;
import de.tu_darmstadt.gdi1.towerdefense.model.TowerDefenseModel;

/**
 * 
 * class extends {@link GamePanel} to add more functionality and write inherited
 * abstract methods
 * 
 */
public class MainFrame extends GamePanel {

	/**
	 * default serial id
	 */
	private static final long serialVersionUID = 1L;
	/**
	 * the reference to the TowerDefenseModel
	 */
	private TowerDefenseModel model;

	/**
	 * constructor
	 * 
	 * @param theParentWindow
	 *            the window in which the panel is drawn
	 */
	public MainFrame(GameWindow theParentWindow) {
		super(theParentWindow);
		registerImages();
		setEnabled(true);
		setVisible(true);
	}

	/**
	 * adds the current game content to the game panel
	 */
	@Override
	protected void setGamePanelContents() {
		char[][] map = model.getCurrentMap();

		for (int i = 0; i < map.length; i++) {
			for (int j = 0; j < map[i].length; j++) {

				if (map[i][j] == '_') {

					Random random = new Random();
					int r = random.nextInt(4);
					String stringValue = Integer.toString(r);

					try {
						placeEntity("field" + stringValue);
					} catch (ParameterOutOfRangeException e) {
						e.printStackTrace();
					}
				}

				if (map[i][j] == '#') {
					try {
						placeEntity("wall");
					} catch (ParameterOutOfRangeException e) {
						e.printStackTrace();
					}
				}

				if (map[i][j] == 'M') {
					
							String path = model.getMonsters().get(0).getMonsterName();;
					try {
						placeEntity("monster_"+path);
					} catch (ParameterOutOfRangeException e) {
						e.printStackTrace();
					}
				}

				if (map[i][j] == 't') {
					try {
						placeEntity("tower");
					} catch (ParameterOutOfRangeException e) {
						e.printStackTrace();
					}
				}

				if (map[i][j] == 'p') {
					try {
						placeEntity("snipertower");
					} catch (ParameterOutOfRangeException e) {
						e.printStackTrace();
					}
				}

				if (map[i][j] == 'S') {
					try {
						placeEntity("spawn");
					} catch (ParameterOutOfRangeException e) {
						e.printStackTrace();
					}
				}

				if (map[i][j] == 'X') {
					String path = "";
					// Die umgebeung durchgen
					if (map[i][j - 1] == '>') {
						// Links
						path = "_r";
					} else if (map[i - 1][j] == '^') {
						// Oben
						path = "_t";

					} else if (map[i][j + 1] == '<') {
						// rechts
						path = "_l";
					} else if (map[i + 1][j] == 'v') {
						// unten
						path = "_d";
					}

					try {
						placeEntity("target" + path);
					} catch (ParameterOutOfRangeException e) {
						e.printStackTrace();
					}
				}

				if (map[i][j] == '>'
						&& ((map[i + 1][j] == '^' || map[i + 1][j] == 'M'
								|| map[i + 1][j] == 'S' || map[i + 1][j] == 'X') && (map[i][j + 1] == '>'
								|| map[i][j + 1] == 'M' || map[i][j + 1] == 'S' || map[i][j + 1] == 'X'))) {
					try {
						placeEntity("path_tr");
					} catch (ParameterOutOfRangeException e) {
						e.printStackTrace();
					}
				} else

				if (map[i][j] == '>'
						&& ((map[i - 1][j] == 'v' || map[i - 1][j] == 'M'
								|| map[i - 1][j] == 'S' || map[i - 1][j] == 'X') && (map[i][j + 1] == '>'
								|| map[i][j + 1] == 'M' || map[i][j + 1] == 'S' || map[i][j + 1] == 'X'))) {
					try {
						placeEntity("path_br");
					} catch (ParameterOutOfRangeException e) {
						e.printStackTrace();
					}
				} else

				if (map[i][j] == '>') {
					try {
						placeEntity("path_lr");
					} catch (ParameterOutOfRangeException e) {
						e.printStackTrace();
					}
				}

				if (map[i][j] == '<'
						&& (map[i + 1][j] == '^' || map[i + 1][j] == 'M'
								|| map[i + 1][j] == 'S' || map[i + 1][j] == 'X')) {
					try {
						placeEntity("path_bl");
					} catch (ParameterOutOfRangeException e) {
						e.printStackTrace();
					}
				} else

				if (map[i][j] == '<'
						&& ((map[i][j - 1] == '<' || map[i][j - 1] == 'M'
								|| map[i][j - 1] == 'S' || map[i][j - 1] == 'X') && (map[i - 1][j] == 'v'
								|| map[i - 1][j] == 'M' || map[i - 1][j] == 'S' || map[i - 1][j] == 'X'))) {

					try {
						placeEntity("path_tl");
					} catch (ParameterOutOfRangeException e) {
						e.printStackTrace();
					}
				} else

				if (map[i][j] == '<') {
					try {
						placeEntity("path_lr");
					} catch (ParameterOutOfRangeException e) {
						e.printStackTrace();
					}
				}

				if (map[i][j] == 'v'
						&& ((map[i + 1][j] == 'v' || map[i + 1][j] == '>'
								|| map[i + 1][j] == 'M' || map[i + 1][j] == 'S' || map[i + 1][j] == 'X') && (map[i][j - 1] == '>'
								|| map[i][j - 1] == 'M' || map[i][j - 1] == 'S' || map[i][j - 1] == 'X'))) {
					try {
						placeEntity("path_bl");
					} catch (ParameterOutOfRangeException e) {
						e.printStackTrace();
					}
				} else

				if (map[i][j] == 'v'
						&& ((map[i + 1][j] == '>' || map[i + 1][j] == 'v'
								|| map[i + 1][j] == '>' || map[i + 1][j] == 'M'
								|| map[i + 1][j] == 'S' || map[i + 1][j] == 'X') && (map[i][j + 1] == '<'
								|| map[i][j + 1] == 'M' || map[i][j + 1] == 'S' || map[i][j + 1] == 'X'))) {
					try {
						placeEntity("path_tr");
					} catch (ParameterOutOfRangeException e) {
						e.printStackTrace();
					}
				} else

				if (map[i][j] == 'v') {
					try {
						placeEntity("path_ud");
					} catch (ParameterOutOfRangeException e) {
						e.printStackTrace();
					}
				}

				if (map[i][j] == '^'
						&& ((map[i][j + 1] == '<' || map[i][j + 1] == 'M'
								|| map[i][j + 1] == 'S' || map[i][j + 1] == 'X') && (map[i - 1][j] == '^'
								|| map[i - 1][j] == '<'
								|| map[i - 1][j] == 'M'
								|| map[i - 1][j] == 'S' || map[i - 1][j] == 'X'))) {

					try {
						placeEntity("path_br");
					} catch (ParameterOutOfRangeException e) {
						e.printStackTrace();
					}
				} else

				if (map[i][j] == '^'
						&& ((map[i][j - 1] == '>' || map[i][j - 1] == 'M'
								|| map[i][j - 1] == 'S' || map[i][j - 1] == 'X') && (map[i - 1][j] == '>'
								|| map[i - 1][j] == '^'
								|| map[i - 1][j] == 'M'
								|| map[i - 1][j] == 'X' || map[i - 1][j] == 'S'))) {

					try {
						placeEntity("path_tl");
					} catch (ParameterOutOfRangeException e) {
						e.printStackTrace();
					}
				} else

				if (map[i][j] == '^') {

					try {
						placeEntity("path_ud");
					} catch (ParameterOutOfRangeException e) {
						e.printStackTrace();
					}
				}

			}
		}
	}

	/**
	 * show the panel after it was resized
	 */
	@Override
	protected void panelResized() {

		setVisible(true);
	}

	/**
	 * register the images in the image list
	 */
	public void registerImages() {
		try {
			// register the monster images
			this.registerImage("monster_black",
					"src/de/tu_darmstadt/gdi1/resources/images/defaultskin/monsters/lemming.jpg");
			this.registerImage(
					"monster_blue",
					"src/de/tu_darmstadt/gdi1/resources/images/defaultskin/monsters/monster_blue.png");
			this.registerImage(
					"monster_mult",
					"src/de/tu_darmstadt/gdi1/resources/images/defaultskin/monsters/monster_mult.png");
			this.registerImage(
					"monster_red",
					"src/de/tu_darmstadt/gdi1/resources/images/defaultskin/monsters/monster_red.png");
			// register the tower images
			this.registerImage("tower",
					"src/de/tu_darmstadt/gdi1/resources/images/defaultskin/towers/tower.png");
			this.registerImage("snipertower",
					"src/de/tu_darmstadt/gdi1/resources/images/defaultskin/towers/splashtower.png");
			this.registerImage("frosttower",
					"src/de/tu_darmstadt/gdi1/resources/images/defaultskin/towers/frosttower.png");
			this.registerImage("aoetower",
					"src/de/tu_darmstadt/gdi1/resources/images/defaultskin/towers/aoetower.png");
			// register the map tiles
			this.registerImage("field0",
					"src/de/tu_darmstadt/gdi1/resources/images/defaultskin/map/field.png");
			this.registerImage("field1",
					"src/de/tu_darmstadt/gdi1/resources/images/defaultskin/map/field1.png");
			this.registerImage("field2",
					"src/de/tu_darmstadt/gdi1/resources/images/defaultskin/map/field2.png");
			this.registerImage("field3",
					"src/de/tu_darmstadt/gdi1/resources/images/defaultskin/map/field3.png");

			this.registerImage("path_bl",
					"src/de/tu_darmstadt/gdi1/resources/images/defaultskin/map/path_bl.png");
			this.registerImage("path_br",
					"src/de/tu_darmstadt/gdi1/resources/images/defaultskin/map/path_br.png");
			this.registerImage("path_lr",
					"src/de/tu_darmstadt/gdi1/resources/images/defaultskin/map/path_lr.png");

			this.registerImage("path_tl",
					"src/de/tu_darmstadt/gdi1/resources/images/defaultskin/map/path_tl.png");
			this.registerImage("path_tr",
					"src/de/tu_darmstadt/gdi1/resources/images/defaultskin/map/path_tr.png");

			this.registerImage("path_ud",
					"src/de/tu_darmstadt/gdi1/resources/images/defaultskin/map/path_ud.png");

			this.registerImage("spawn",
					"src/de/tu_darmstadt/gdi1/resources/images/defaultskin/map/spawn.png");
			this.registerImage("target",
					"src/de/tu_darmstadt/gdi1/resources/images/defaultskin/map/target.png");
			this.registerImage("target_d",
					"src/de/tu_darmstadt/gdi1/resources/images/defaultskin/map/target_d.png");
			this.registerImage("target_l",
					"src/de/tu_darmstadt/gdi1/resources/images/defaultskin/map/target_l.png");
			this.registerImage("target_r",
					"src/de/tu_darmstadt/gdi1/resources/images/defaultskin/map/target_r.png");
			this.registerImage("target_t",
					"src/de/tu_darmstadt/gdi1/resources/images/defaultskin/map/target_t.png");
			this.registerImage("wall",
					"src/de/tu_darmstadt/gdi1/resources/images/defaultskin/map/wall.png");

		} catch (ParameterOutOfRangeException e) {
			e.printStackTrace();
		} catch (InvalidOperationException e) {
			e.printStackTrace();
		} catch (InternalFailureException e) {
			e.printStackTrace();
		}
	}

	/**
	 * add a reference to the tower defense model to the game panel to get the
	 * models information for the view
	 * 
	 * @param m
	 */
	public void addModel(TowerDefenseModel m) {
		model = m;
	}

}
